/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converter;

import entity.Foto;
import entity.Prodotto;
import java.util.StringTokenizer;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Antonio
 */
@FacesConverter(forClass = Prodotto.class)
public class ProdottoConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        StringTokenizer st = new StringTokenizer(value, "#?=");
        Prodotto prod = new Prodotto();
        Foto tmp = new Foto();
        int i = 0;

        while (st.hasMoreTokens()) {

            String token = st.nextToken();

            switch (i) {

                case 0:
                    prod.setcodiceBarre(Long.parseLong(token));
                    break;
                case 1:
                    prod.setNome(token);
                    break;
                case 2:
                    prod.setDescrizione(token);
                    break;
                case 3:
                    tmp.setId(Long.parseLong(token));
                    break;
            }
            i++;
        }
        prod.setFoto(tmp);
        return prod;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value != null) {

            return value.toString();
        }
        System.out.println("value==null");
        return null;
    }
}
